package com.book.workbench.controller;

import com.book.common.Result;
import com.book.settings.pojo.User;
import com.book.settings.service.UserService;
import com.book.workbench.pojo.Book;
import com.book.workbench.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BookController {

    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;

    @PostMapping("/workbench/book/add.do")
    public Result addBook(@RequestBody Book book){
        int addBookResult = bookService.addBook(book);
        if(addBookResult==1){
            return Result.success();
        }else if(addBookResult==-1){
            return Result.error("添加的书籍已经存在");
        }else{
            return Result.error("系统错误");
        }
    }

    @PostMapping("/workbench/book/del.do")
    public Result deleteBook(@RequestBody Book book){
        int deleteBookResult = bookService.deleteBookById(book);
        if(deleteBookResult==1){
            return Result.success();
        }else{
            return Result.error("书籍无法删除");
        }
    }

    @GetMapping("/workbench/book/search.do")
    public Result searchBook( Book book){
        PageInfo<Book> bookPageInfo = bookService.getListBook(book);
        int countListBook = bookService.getCountListBook(book);
        return Result.successBooks(bookPageInfo.getList(),countListBook);
    }

    @PostMapping("/workbench/book/update.do")
    public Result updateBook(@RequestBody Book book){
        int updateBookById = bookService.updateBookById(book);
        if(updateBookById==1){
            return Result.success();
        }else{
            return Result.error("未找到要修改的书籍");
        }
    }

    @GetMapping("/workbench/book/searchall.do")
    public Result searchAllBook( Book book){
        PageInfo<Book> bookPageInfo = bookService.getListAllBook(book);
        int countListAllBook = bookService.getCountListAllBook(book);
        return Result.successBooks(bookPageInfo.getList(),countListAllBook);
    }

    @GetMapping("/workbench/info/canborr.do")
    public Result searchBookBy( Book book){
        PageInfo<Book> bookPageInfo = bookService.getListBookBy(book);
        int countListBookBy = bookService.getCountListBookBy(book);
        return Result.successBooks(bookPageInfo.getList(),countListBookBy);
    }

    @GetMapping("/workbench/infor/canback.do")
    public Result searchUserNotReturnBook( User user){
        User user1 = bookService.getUserByAct(user.getAct());
        if(user1==null){
            return Result.error("没有该用户");
        }else{
            List<Book> bookList = bookService.searchUserNotReturnBook(user1.getId());
            return Result.successBooks1(bookList,user1.getId(),user1.getName());
        }
    }

    @GetMapping("/workbench/book/searchbyid.do")
    public Book searchBookById(String id){
        Book book = bookService.searchBookById(id);
        return book;
    }

}
